package com.boyon.system.provider;

import java.util.List;

import org.hibernate.type.LongType;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boyon.common.status.AccountEnmu;
import com.boyon.common.tools.CollectionTool;
import com.boyon.domain.AccountData;
import com.boyon.hbm.criteria.OperationType;
import com.boyon.hbm.criteria.Param;
import com.boyon.hbm.criteria.Query;
import com.boyon.service.AccountProviderService;
import com.boyon.system.entity.Account;
import com.boyon.system.service.AccountService;

/**
 * ClassName:AccountProviderServiceImpl <br/>
 * Function: AccountProviderService接口实现类. <br/>
 * Date: 2018年4月3日 下午3:48:32 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Service(value = "accountProviderService")
public class AccountProviderServiceImpl implements AccountProviderService {

	@Autowired
	private AccountService accountService;

	/**
	 * 创建账号.
	 * 
	 * @see com.boyon.service.AccountCreateService#createAccount(com.boyon.domain.AccountData)
	 */
	public void createAccount(AccountData accountData) {
		Account account = new Account();
		account.setAccount(accountData.getAccountNo());
		account.setRoleId(accountData.getRoleId());
		account.setAccountType(accountData.getAccountType());
		accountService.createAccount(account,
				AccountEnmu.getType(accountData.getRelationType()),
				accountData.getRelationId());
	}

	/**
	 * 重置密码.
	 * 
	 * @see com.boyon.service.AccountCreateService#resetPass(java.lang.Long)
	 */
	public void resetPass(Long relationId, Integer relationType) {
		Query query = new Query();
		query.addParam(new Param<Integer>("relationType", OperationType.eq,
				relationType));
		query.addParam(new Param<Object[]>(
				"exists(select sys_account_relation.account_id from sys_account_relation where this_.account_id=sys_account_relation.account_id and relation_id=?)",
				OperationType.sql, new Object[] { relationId },
				new Type[] { LongType.INSTANCE }));
		List<Account> list = accountService.query(query);
		if (CollectionTool.isNotEmpty(list) && list.size() == 1) {
			Account account = list.get(0);
			accountService.resetPassword(account);
		}
	}

}
